﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DNA.Mvc
{
    public class HtmlElementFactory
    {
        private HtmlHelper html;
        private AjaxHelper ajax;

        public HtmlHelper Html
        {
            get { return html; }
        }

        public HtmlElementFactory(HtmlHelper helper)
        {
            html = helper;
            ajax = new AjaxHelper(html.ViewContext, html.ViewDataContainer);
        }

        public MvcHtmlString PageHeader()
        {
            var context = WebSiteContext.Current;
            var web =context.Web;
            if (web == null) web = context.RootWeb;

            var urlHelper = UrlUtility.CreateUrlHelper();
            string defaultUrl = string.IsNullOrEmpty(web.DefaultUrl) ?  context.ApplicationPath : urlHelper.Content(web.DefaultUrl);
            
            if ((!web.IsRoot) && (string.IsNullOrEmpty(web.DefaultUrl)))
            {
                defaultUrl = UrlResolver.Resolve(web)+"/index.html";
            }

            var header = new TagBuilder("div");
            var homeLink = new TagBuilder("a");
            var h1 = new TagBuilder("h1");

            header.GenerateId("dna-page-header");
            homeLink.MergeAttribute("href", defaultUrl);

            if (!string.IsNullOrEmpty(web.LogoImageUrl))
            {
                var logo = new TagBuilder("img");
                logo.MergeAttribute("alt", web.Title);
                logo.MergeAttribute("src", urlHelper.Content(web.LogoImageUrl));
                logo.MergeAttribute("style", "float:left;border:none;");
                homeLink.InnerHtml = logo.ToString(TagRenderMode.SelfClosing);
            }

            switch (web.LogoLayout)
            {
                case LogoLayouts.LogoOnly:
                    header.InnerHtml = homeLink.ToString();
                    break;
                case LogoLayouts.LogoAndTitle:
                    header.InnerHtml = "<table><tr><td>" + homeLink.ToString() + "</td><td><h1>";
                    homeLink.InnerHtml = web.Title;
                    header.InnerHtml+=homeLink.ToString() + "</h1></td></tr></table>";
                    break;
                case LogoLayouts.LogoWithTitleRightBottom:
                    header.InnerHtml = "<table><tr><td>" + homeLink.ToString() + "</td><td style=\"vertical-align:bottom;\"><h1>";
                    homeLink.InnerHtml = web.Title;
                    header.InnerHtml += homeLink.ToString() + "</h1></td></tr></table>";
                    break;
                case LogoLayouts.LogoWithTitleRightTop:
                    header.InnerHtml = "<table><tr><td>" + homeLink.ToString() + "</td><td style=\"vertical-align:top;\"><h1>" ;
                    homeLink.InnerHtml = web.Title;
                    header.InnerHtml += homeLink.ToString() + "</h1></td></tr></table>";
                    break;
                case LogoLayouts.LogoWithTitleBlow:
                    header.InnerHtml = "<table><tr><td>" + homeLink.ToString() + "</td></tr><tr><td><h1>";
                    homeLink.InnerHtml = web.Title;
                    header.InnerHtml += homeLink.ToString() + "</h1></td></tr></table>";
                    break;
                default:
                    homeLink.InnerHtml = web.Title;
                    header.InnerHtml = "<h1>" + homeLink.ToString() + "</h1>";
                    break;
            }

            return MvcHtmlString.Create(header.ToString());
        }
    }
}